<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>鲜花信息管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css" tppabs="http://res.layui.com/layui/dist/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="batchDel">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="writeExcel">批量导出excel</button>
        <button class="layui-btn layui-btn-sm" lay-event="getExcelTemplate">获取Excel模板</button>
        <button type="button" class="layui-btn layui-btn-sm" id="test1">
            <i class="layui-icon">&#xe67c;</i>导入Excel文件
        </button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function(){
        var table = layui.table;

        table.render({
            elem: '#test'
            ,url:'flower/findByPage'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '鲜花数据表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'编号', width:80, fixed: 'left', unresize: true, sort: true}
                ,{field:'name', title:'名称', width:120, edit: 'text'}
                ,{field:'price', title:'价格', width:120, edit: 'text'}
                ,{field:'count', title:'库存', width:120, edit: 'text'}
                ,{field:'text', title:'详情', width:120, edit: 'text'}
                ,{field:'createTime', title:'创建时间', width:120, templet:"<div> {{layui.util.toDateString(d.createTime,'yyyy-MM-dd') }} </div>"}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
            ]]
            //,page: true
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                ,curr: 1 //设定初始在第 1 页
                ,groups: 3 //只显示 3 个连续页码
                ,first: "首页" //显示首页
                ,last: "末页" //显示尾页
                ,limit: 6
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'batchDel':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));
                    //获取选中数据的id
                    var ids = "";
                    for(var i = 0; i < data.length; i++){
                        ids += "&ids=" + data[i].id;
                    }
                    alert("批量删除的ids：" + ids);
                    $.get("/flower/batchDel", ids,
                        function(data){
                            //alert(data);
                            if("success" == data){
                                table.reload("test",{});//重新加载当前表格
                            }
                        });
                    break;
                case 'writeExcel':
                    var data = checkStatus.data;
                    //获取选中数据的id
                    if(data.length < 1){
                        layer.msg('未选中数据');
                        break;
                    }
                    var ids = "";
                    for(var i = 0; i < data.length; i++){
                        ids += "&ids=" + data[i].id;
                    }
                    //location对象的href属性表示跳转到指定路径
                    location.href = "/flower/wirteExcel?ids="+ids
                    break;
                case 'getExcelTemplate':
                    //layer.msg(checkStatus.isAll ? '全选': '未全选');
                    location.href = "/excel/flower.xlsx"
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            console.log(data);
            console.log(data.id+"-----------------");
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();//删除表结构
                    layer.close(index);
                    var deleteId = data.id;
                    //alert(deleteId);
                    //去数据库中删除（真正的删除）1.物理删除 2.逻辑删除
                    //向后台发送请求（ajax异步请求）
                    $.get("/flower/deleteById", { id: deleteId },
                        function(data){
                            //alert(data);
                            if("success" == data){
                                table.reload("test",{});//重新加载当前表格
                            }
                        });
                });
            } else if(obj.event === 'edit'){
                // layer.prompt({
                //     formType: 2
                //     ,value: data.email
                // }, function(value, index){
                //     obj.update({
                //         email: value
                //     });
                //     layer.close(index);
                // });

                //由于layui已经帮我们把当前单个对象查询出来了，我们之需要把对象显示出来即可
                queryOne(data);
            }
        });
    });
</script>

</body>

<script>
    function queryOne(data){
        layer.open({
            type:1,
            content: $("#updateFlowerForm").html(),
            title: ['修改页面', 'font-size:18px;'],
            area: ['600px', '600px'],
            success: function(){
                $("#id").val(data.id);
                $("#name").val(data.name);
                $("#price").val(data.price);
                $("#count").val(data.count);
                $("#text").val(data.text);
                $("#createTime").val(layui.util.toDateString(data.createTime,'yyyy-MM-dd'));
                //渲染页面
                layui.form.render();
            },
            btn: ['提交', '取消']
            ,yes: function(index, layero){
                //修改操作
                $.get("/flower/updateFlower", $("#updateFlower").serialize(),
                    function(data){
                        if("success" == data){
                            location.reload();//刷新当前页面
                        }
                    });
            }
            ,btn2: function(index, layero){
                alert("取消")
            }
        });
    }
</script>

<script type="text/html" id="updateFlowerForm">
    <form class="layui-form" id="updateFlower">
        <!-- 把id 作为隐藏域传过去 -->
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-inline">
                <input type="text" name="price" id="price" required  lay-verify="required" placeholder="请输入价格" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-inline">
                <input type="text" name="count" id="count" required  lay-verify="required" placeholder="请输入库存" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">详情</label>
            <div class="layui-input-inline">
                <input type="text" name="text" id="text" required  lay-verify="required" placeholder="请输入详情" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">创建时间</label>
                <div class="layui-input-inline">
                    <input type="date" name="createTime" id="createTime" lay-verify="date" placeholder="创建时间" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
    </form>
</script>

<script>
    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,accept: 'file'
            ,url: '/flower/upload' //上传接口
            ,done: function(res){
                if(res.msg == "success"){
                    alert("数据导入成功！！")
                    location.reload();//刷新当前页面
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>

</html>